package org.example.algorithm;

import java.util.ArrayList;
import java.util.List;

/**
 *  228. 汇总区间
 */

public class SummaryRangesSolution {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 5};
        List<String> res = summaryRanges(nums);
        System.out.println(res);
    }

    public static List<String> summaryRanges(int[] nums) {
        if (nums.length == 0) {
            return new ArrayList<>();
        }
        List<String> res = new ArrayList<>();
        int start = nums[0];
        int end = nums[0];
        for (int i=1; i<nums.length; i++) {
            if (nums[i] == nums[i-1] + 1) {
                end = nums[i];
            } else {
                if (start == end) {
                    res.add(String.valueOf(start));
                } else {
                    res.add(start + "->" + end);
                }
                start = nums[i];
                end = nums[i];
            }
        }
        if (start == end) {
            res.add(String.valueOf(start));
        } else {
            res.add(start + "->" + end);
        }
        return res;
    }
}
